﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Medicalsystem.MCP.FormDesigner.Common;
using Medicalsystem.MCP.FormDesigner.Define;
using Medicalsystem.MCP.FormDesigner.Interface;
using Medicalsystem.MCP.FormDesigner.Property;

namespace Medicalsystem.MCP.FormDesigner.MyControls
{
    /// <summary>
    /// 多行文本输入框
    /// </summary>
    [ToolboxItem(false)]
    public class MyDataGrid : DataGridView, ICtrlUI
    {
        public MyDataGrid() { }

        public MyDataGrid(Model model) : this(model, EDesignModeState.Run) { }

        public MyDataGrid(Model model, EDesignModeState designModeState)
        {
            this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            this._model = model;
            this._designModeState = designModeState;
            this.DefaultCellStyle.SelectionBackColor = System.Drawing.Color.White;
            _myColumns = new List<MyDataGridColumn>();
        }

        private EDesignModeState _designModeState = EDesignModeState.Run;
        private Model _model;
        private string _showText = string.Empty;
        private List<MyDataGridColumn> _myColumns = null;

        protected GetCtrlProperty _propertyObject = null;
        protected bool _visibleZ = true;

        public override System.Drawing.Color BackColor
        {
            get
            {
                return this.BackgroundColor;
            }
            set
            {
                this.BackgroundColor = value;
            }
        }

        #region ICtrlUI 成员

        public bool VisibleZ
        {
            get
            {
                return _visibleZ;
            }
            set
            {
                _visibleZ = value;
            }
        }

        public EBorderStyle BorderStyleZ
        {
            get
            {
                return EBorderStyle.None;
            }
            set
            {
            }
        }

        public string ShowText
        {
            get
            {
                return _showText;
            }
            set
            {
                _showText = value;
            }
        }

        #endregion ICtrlUI 成员

        #region ICtrlBase 成员

        public string CtrlID
        {
            get
            {
                return this.Name;
            }
            set
            {
                this.Name = value;
            }
        }

        public EDesignModeState DesignModeState
        {
            get
            {
                return _designModeState;
            }
            set
            {
                _designModeState = value;
            }
        }

        public GetCtrlProperty PropertyObject
        {
            get
            {
                if (this._propertyObject == null)
                {
                    this._propertyObject = new GetMyDataGridProperty(this);
                }

                return this._propertyObject;
            }
        }

        public EUISizeMode UISizeMode
        {
            get { return EUISizeMode.AllowBoth; }
        }

        public Model Model
        {
            get
            {
                return _model;
            }
        }

        #endregion ICtrlBase 成员

        #region IXMLConvertor 成员

        public string ToXML()
        {
            MyDataGridPropertyXML XML = new MyDataGridPropertyXML(this);
            return XML.ToXML();
        }

        public void FromXML(string strXML)
        {
            if (strXML.Length > 0)
            {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.LoadXml(strXML);

                FromXML(doc.DocumentElement.ChildNodes);
            }
        }

        public void FromXML(System.Xml.XmlNodeList xns)
        {
            if (xns != null && xns.Count > 0)
            {
                MyDataGridPropertyXML XML = new MyDataGridPropertyXML(this);
                XML.FromXML(xns);
            }
        }

        #endregion IXMLConvertor 成员

        public List<MyDataGridColumn> MyColumns
        {
            get { return _myColumns; }
            set
            {
                if (value != null)
                {
                    _myColumns = value;

                    InitColumns();
                }
            }
        }

        public void InitColumns()
        {
            this.Columns.Clear();

            foreach (MyDataGridColumn col in _myColumns)
            {
                DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
                column.HeaderText = col.HeaderText;
                column.Width = col.Width;
                this.Columns.Add(column);
            }
        }
        #region ICtrlBase 成员


        public System.Drawing.Image IcoForToolBox
        {
            get { return Properties.Resources.PropertiesHS; }
        }

        #endregion
    }
}